package cn.wzptsoft.tinymallbe.service;

import cn.wzptsoft.tinymallbe.entity.Address;
import cn.wzptsoft.tinymallbe.entity.Order;
import cn.wzptsoft.tinymallbe.entity.OrderItem;
import cn.wzptsoft.tinymallbe.entity.Sku;
import cn.wzptsoft.tinymallbe.repository.AddressRepository;
import cn.wzptsoft.tinymallbe.repository.OrderItemRepository;
import cn.wzptsoft.tinymallbe.repository.OrderRepository;
import cn.wzptsoft.tinymallbe.repository.SkuRepository;
import cn.wzptsoft.tinymallbe.vo.ItemsVO;
import cn.wzptsoft.tinymallbe.vo.OrderVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;

@Service
public class OrderService {
    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private AddressRepository addressRepository;

    @Autowired
    private SkuRepository skuRepository;

    @Autowired
    private OrderItemRepository orderItemRepository;

    public List<Order> statusAll() {
        List<Order> allOrders  = orderRepository.findAll();
        return  allOrders ;
    }

    public List<Order> statusid(Integer id) {
//        0 待支付  1 待发货  2 待收货  4 取消
        List<Order> o  =orderRepository.findByStatus(id);
        return o;
    }

    public Order add(OrderVO obj) throws JsonProcessingException {
//        总价格
        double zongjiage=0;
//        总数量
        Integer zongsum=0;

//        先创建一个order
        Order order = new Order();
        Order save = orderRepository.save(order);
//        拿到这个数据的主键 id
        Long zhuid = save.getId();

//        地址id 设置地址
        Long addressId = obj.getAddressId();
        Optional<Address> byId = addressRepository.findById(addressId);
        order.setSnapAddress(new ObjectMapper().writeValueAsString(byId.get()));

//        设置items  先通过obj.items获取商品skuid和count 再一个一个添加进去
//        ArrayList<OrderItem> items = new ArrayList<>();
//        循环传进来的obj.items 再根据skuRepository方法获取每一个商品信息 拿到信息放进items
        for (int i = 0; i <obj.items.size(); i++) {
            //        add添加的是商品skuid的对象
            Optional<Sku> byId1 = skuRepository.findById(obj.items.get(i).getSkuId());
            //单个价格
            Integer a=byId1.get().getPrice().intValue();

            zongjiage=a*obj.items.get(i).getCount();
            zongsum+=obj.items.get(i).getCount();

            OrderItem orderItem=new OrderItem();
            orderItem.setCount(obj.items.get(i).getCount());
            orderItem.setOrderId(zhuid);
            orderItem.setSkuId(obj.items.get(i).getSkuId());
            orderItem.setSnapImage(byId1.get().getImg());
            orderItem.setSnapPrice(byId1.get().getPrice());
            orderItem.setSnapTitle(byId1.get().getTitle());
            orderItem.setSpuId(byId1.get().getSpuId());
            orderItemRepository.save(orderItem);
//            items.add(orderItem);
        }
//        order.setItems(items);

        order.setTotalAmount(zongjiage);
        order.setTotalCount(zongsum);
        order.setStatus(0);
        order.setUserId(1);
        order.setId(zhuid);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = formatter.format(calendar.getTime());
        order.setOrderNo(format);
        Order save2 = orderRepository.save(order);
        return save2;
    }


    public String zhifu(Long id) {
        Optional<Order> byId = orderRepository.findById(id);
        if (byId.isPresent()){
            Order order = byId.get();
            //设置成已支付
            order.setStatus(1);
            orderRepository.save(order);
            return "支付成功";
        }else {
            return "未找到该订单";
        }
    }

    public String quxiao(Long id) {
        Optional<Order> byId = orderRepository.findById(id);
        if (byId.isPresent()){
            Order order = byId.get();
            //设置成已支付
            order.setStatus(3);
            orderRepository.save(order);
            return "取消成功";
        }else {
            return "未找到该订单";
        }
    }
}
